package zhang.lao.console.service;/**
 * Created by zhangqin on 2017/10/10.
 */

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import zhang.lao.build.mybatis.jdbc.auto.model.*;
import zhang.lao.build.tool.ListUtils;
import zhang.lao.build.tool.UUIDTool;
import zhang.lao.build.tool.date.DateUtil;
import zhang.lao.console.controller.wn.excel.ExcelReader;
import zhang.lao.dao.base.*;
import zhang.lao.extents.spring.exception.ConsoleException;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultEnum;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.service.console.SysSequenceFactory;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @create 2017-10-10 15:57
 **/
@Service
public class ImportGt06Service {

    @Autowired
    private WnMachineDeviceDao wnMachineDeviceDao;
    @Autowired
    private WnMachineDao wnMachineDao;
    @Autowired
    private WnMachineToolDao wnMachineToolDao;
    @Autowired
    private WnMachineToolLinkDao wnMachineToolLinkDao;
    @Autowired
    private WnMachineDeviceLinkDao wnMachineDeviceLinkDao;
    @Autowired
    private WnMachineDeviceImeiDao wnMachineDeviceImeiDao;
    @Resource
    private SysSequenceFactory sysSequenceFactory;

    @Transactional
    public  HttpResult upExcel(InputStream inputStream){
        List<Map<String, String>> mapList = new ExcelReader().readExcelContent(inputStream);
        for (Map<String, String> stringStringMap : mapList) {
            String sbbh = stringStringMap.get("SBBH");
            String IMEI = stringStringMap.get("IMEI");
            String SIMCARD = stringStringMap.get("SIMCARD");
            String SIMYXQ = stringStringMap.get("SIMYXQ");
            String SSJD_ID = stringStringMap.get("SSJD_ID");
            WnMachineDeviceExample wnMachineExample = new WnMachineDeviceExample();
            wnMachineExample.createCriteria().andDeviceNumberEqualTo(sbbh);
            if(wnMachineDeviceDao.countByExample(wnMachineExample)>0){
                throw new ConsoleException(HttpResultEnum.DEVICE_NUMBER_ERR);
            }
            WnMachineDevice wnMachineDevice = new WnMachineDevice();
            wnMachineDevice.setCreateTime(new Date());
            wnMachineDevice.setCreateUser("admin");
            wnMachineDevice.setDeviceNumber(sbbh);
            wnMachineDevice.setId(sysSequenceFactory.getSequence("wn_machine_device"));
            wnMachineDevice.setDeviceType("gt06");
            wnMachineDevice.setFarmId(SSJD_ID);
            wnMachineDeviceDao.insertSelective(wnMachineDevice);
            WnMachineDeviceImei  wnMachineDeviceImei = new WnMachineDeviceImei();
            wnMachineDeviceImei.setBuyTime(new Date());
            if(StringUtils.isBlank(SIMYXQ)){
                wnMachineDeviceImei.setValidityPeriod(new Date());
            }else{
                wnMachineDeviceImei.setValidityPeriod(DateUtil.StringToDate(SIMYXQ));
            }
            wnMachineDeviceImei.setId(UUIDTool.getUUID());
            wnMachineDeviceImei.setImei(IMEI);
            wnMachineDeviceImei.setDeviceId(wnMachineDevice.getId());
            wnMachineDeviceImei.setSimcard(SIMCARD);
            wnMachineDeviceImeiDao.insertSelective(wnMachineDeviceImei);
        }
        return HttpResultUtil.buildSuccess();
    }

    @Transactional
    public  HttpResult upExcelMachine(InputStream inputStream){
        List<Map<String, String>> mapList = new ExcelReader().readExcelContent(inputStream);
        for (Map<String, String> stringStringMap : mapList) {
            String sbbh = stringStringMap.get("SBBH");
            String FK = stringStringMap.get("FK");
            String CPH = stringStringMap.get("CPH");
            WnMachineDeviceExample wnMachineDeviceExample = new WnMachineDeviceExample();
            wnMachineDeviceExample.createCriteria().andDeviceNumberEqualTo(sbbh);
            WnMachineDevice first = ListUtils.getFirst(wnMachineDeviceDao.selectByExample(wnMachineDeviceExample));
            if(first!=null){
                WnMachineExample wnMachineExample = new WnMachineExample();
                wnMachineExample.createCriteria().andLicensePlateNumberEqualTo(CPH);
                if(wnMachineDao.countByExample(wnMachineExample)>0){
                    throw new ConsoleException(HttpResultEnum.DEVICE_NUMBER_ERR);
                }
                Integer id = first.getId();
                WnMachine wnMachine = new WnMachine();
                wnMachine.setBuyTime(new Date());
                wnMachine.setCreateTime(new Date());
                wnMachine.setFarmId(first.getFarmId());
                wnMachine.setId(UUIDTool.getUUID());
                wnMachine.setLicensePlateNumber(CPH);
                wnMachineDao.insertSelective(wnMachine);
                WnMachineDeviceLink wnMachineDeviceLink = new WnMachineDeviceLink();
                wnMachineDeviceLink.setDeviceId(id);
                wnMachineDeviceLink.setMachineId(wnMachine.getId());
                wnMachineDeviceLink.setId(UUIDTool.getUUID());
                wnMachineDeviceLinkDao.insertSelective(wnMachineDeviceLink);
                WnMachineTool wnMachineTool = new WnMachineTool();
                if(StringUtils.isBlank(FK)){
                    wnMachineTool.setBreadth(new Double(1));
                }else{
                    wnMachineTool.setBreadth(new Double(FK));
                }

                wnMachineTool.setBuyTime(new Date());
                wnMachineTool.setFarmId(wnMachine.getFarmId());
                wnMachineTool.setName(CPH+"自带机具");
                wnMachineTool.setId(UUIDTool.getUUID());
                wnMachineToolDao.insertSelective(wnMachineTool);
                WnMachineToolLink wnMachineToolLink = new WnMachineToolLink();
                wnMachineToolLink.setId(UUIDTool.getUUID());
                wnMachineToolLink.setMachineToolId(wnMachineTool.getId());
                wnMachineToolLink.setMachineId(wnMachine.getId());
                wnMachineToolLinkDao.insertSelective(wnMachineToolLink);
            }

        }
        return HttpResultUtil.buildSuccess();
    }

}
